package com.oktest.measure.ui.dialog

import android.content.Context
import android.text.InputType
import android.text.method.DigitsKeyListener
import android.view.View
import com.cali.common.kt.toast
import com.cali.common.kt.value
import com.oktest.measure.R
import kotlinx.android.synthetic.main.measure_dialog_include_input.view.*

/**
 * @Author HaoXin
 * @Date 2020/12/3
 */
class InputDialog(val context: Context, val content: String = "") {
    // 输入对话框
    private val editLayout by lazy {
        View.inflate(context, R.layout.measure_dialog_include_input, null).also { input ->
            input.ivClear.setOnClickListener {
                input.etInput.setText("")
            }
        }
    }

    private val dialogHelper by lazy {
        BaseDialogHelper(context)
    }

    fun show(
        title: String = "请输入",
        confirm: (String) -> Unit
    ): BaseDialogHelper {
        editLayout.etInput.setText(content)
        editLayout.etInput.keyListener = DigitsKeyListener(true, true)
        dialogHelper.show(editLayout, title)
        dialogHelper.onConfirm = {
            val value = editLayout.etInput.value()
            if (value.isNotEmpty()) {
                confirm(value)
            } else {
                context.toast("内容不能为空")
            }
            value.isNotEmpty()
        }
        return dialogHelper
    }
}